$(document).ready(function(){
	loadAll();
	$("#btn_simpan").click(function(){
		$.getJSON('controller/cont.input_kartu.php',$("#frm_kartu").serialize(),function(data){
			if(data.msg){
				if(data.input){
					goAdd();
					$("#nip_detail").html(data.cuti.nip);
	                $("#nama_detail").html(data.pegawai.nama);
	                $("#tgl_detail").html(formattedDate(data.cuti.tglAwal)+" s.d. "+formattedDate(data.cuti.tglAkhir));
	                $("#jns_detail").html(data.ref);
				}else if(data.ubah){
					goAdd();
					$("#nip_detail").html(data.cuti.nip);
	                $("#nama_detail").html(data.pegawai.nama);
	                $("#tgl_detail").html(formattedDate(data.cuti.tglAwal)+" s.d. "+formattedDate(data.cuti.tglAkhir));
	                $("#jns_detail").html(data.ref);
	                document.getElementById("tgl_surat").value = formattedDate(data.kartu.tglSurat);
				    document.getElementById("nomor_surat").value = data.kartu.nomorSurat;
				    document.getElementById("frm_aksi").value = "simpan_edit";
				    document.getElementById("id_aksi").value = data.kartu.id;
				}else {
                    document.getElementById("sukses").innerHTML = data.sukses;
                    $("#div_sukses").fadeIn(500).delay(3500).fadeOut(500);
                    loadAll();
                    goUndo();
                }
			}else {
                document.getElementById("error").innerHTML = data.eror;
                $("#div_error").fadeIn(500).delay(3500).fadeOut(500);
                goUndo();
            }
		});
		return false;
	});

	
});

function loadAll(){
	$.getJSON('controller/cont.kartu.php',{aksi_get_cuti_for_kartu:true},function(data){
		var tab="";
		$('#list_belum_dikartu tbody').html('');
		if(data.msg){
			$.each(data.cuti, function(index, cuti) {				
				tab += "<tr>";
                tab += "<td>" + cuti.pegawai.nip + "</td>";
                tab += "<td>" + cuti.pegawai.nama + "</td>";
                tab += "<td>" + cuti.cuti.tglAju + "</td>";
                tab += "<td>" + formattedDate(cuti.cuti.tglAwal) +" s.d. "+formattedDate(cuti.cuti.tglAkhir)+ "</td>";
                if(cuti.isBatal){
                	tab += "<td>" + cuti.cuti.jmlHari + "(Batal :"+cuti.batal.jmlHariBatal+")</td>";
                }else{
                	tab += "<td>" + cuti.cuti.jmlHari + "</td>";
                }                
                tab += "<td>" + cuti.ref + "</td>";
                if(cuti.isBatal){
                	tab += "<td align=\"center\"><input id=\"selectLibur\" name=\"selectLibur\" type=\"radio\" value=\""+ cuti.cuti.id +",belum," +cuti.batal.id+ "\"/></td>";
                }else{
                	tab += "<td align=\"center\"><input id=\"selectLibur\" name=\"selectLibur\" type=\"radio\" value=\""+cuti.cuti.id +",belum"+ "\"/></td>";
                }                
                tab += "</tr>";
             //    if(cuti.isBatal){
            	// 	tab += "<tr>";
	            //     tab += "<td>" + cuti.pegawai.nip + "</td>";
	            //     tab += "<td>" + cuti.pegawai.nama + "</td>";
	            //     tab += "<td>" + cuti.cuti.tglAju + "</td>";
	            //     tab += "<td>" + formattedDate(cuti.batal.tglAwal) +" s.d. "+formattedDate(cuti.batal.tglAkhir)+ "</td>";
	            //     tab += "<td>(" + cuti.batal.jmlHariBatal + ")</td>";
	            //     tab += "<td>" + cuti.ref + "</td>";
	            //     tab += "<td align=\"center\"><input id=\"selectLibur\" name=\"selectLibur\" type=\"radio\" value=\""+ cuti.cuti.id +",belum," +cuti.batal.id+ "\"/></td>";
	            //     tab += "</tr>";
            	// }      		
            });

		}else{
			tab += "<tr>"
            tab += "<td colspan=\"7\">Data Tidak Ada</td>";
            tab += "</tr>";
		}
		$('#list_belum_dikartu tbody').append(tab);
	});

	$.getJSON('controller/cont.kartu.php',{aksi_get_cuti_sudah_kartu:true},function(data){
		var tab="";
		$('#list_sudah_dikartu tbody').html('');
		if(data.msg){
			$.each(data.cuti, function(index, cuti) {				
				tab += "<tr>";
                tab += "<td>" + cuti.pegawai.nip + "</td>";
                tab += "<td>" + cuti.pegawai.nama + "</td>";
                tab += "<td>" + cuti.cuti.tglAju + "</td>";
                tab += "<td>" + formattedDate(cuti.cuti.tglAwal) +" s.d. "+formattedDate(cuti.cuti.tglAkhir)+ "</td>";
                if(cuti.isBatal){
                	tab += "<td>" + cuti.cuti.jmlHari + "(Batal :"+cuti.batal.jmlHariBatal+")</td>";
                }else{
                	tab += "<td>" + cuti.cuti.jmlHari + "</td>";
                }                
                tab += "<td>" + cuti.ref + "</td>";
                if(cuti.isBatal){
                	tab += "<td align=\"center\"><input id=\"selectLibur\" name=\"selectLibur\" type=\"radio\" value=\""+ cuti.cuti.id +",belum," +cuti.batal.id+ "\"/></td>";
                }else{
                	tab += "<td align=\"center\"><input id=\"selectLibur\" name=\"selectLibur\" type=\"radio\" value=\""+cuti.cuti.id +",belum"+ "\"/></td>";
                }                
                tab += "</tr>";
             //    if(cuti.isBatal){
            	// 	tab += "<tr>";
	            //     tab += "<td>" + cuti.pegawai.nip + "</td>";
	            //     tab += "<td>" + cuti.pegawai.nama + "</td>";
	            //     tab += "<td>" + cuti.cuti.tglAju + "</td>";
	            //     tab += "<td>" + formattedDate(cuti.batal.tglAwal) +" s.d. "+formattedDate(cuti.batal.tglAkhir)+ "</td>";
	            //     tab += "<td>(" + cuti.batal.jmlHariBatal + ")</td>";
	            //     tab += "<td>" + cuti.ref + "</td>";
	            //     tab += "<td align=\"center\"><input id=\"selectLibur\" name=\"selectLibur\" type=\"radio\" value=\""+ cuti.cuti.id +",sudah," +cuti.batal.id + "\"/></td>";
	            //     tab += "</tr>";
            	// }      		
            });

		}else{
			tab += "<tr>"
            tab += "<td colspan=\"7\">Data Tidak Ada</td>";
            tab += "</tr>";
		}
		$('#list_sudah_dikartu tbody').append(tab);
	});
}
function formattedDate(date) {
    var d = new Date(date || Date.now()),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [day, month, year].join('-');
}